$(function () {
    $("#schedule_datagrid").datagrid({
        fit: true,
        rownumbers: true,
        singleSelect: true,
        pagination: true,
        pageSize: 10,
        title: "选课情况",
        url: 'schedule/list/current',
        view: detailview,
        toolbar: [
            {
                text: '查找',
                iconCls: 'icon-search',
                handler: function () {
                    $("#search_dialog").dialog({
                        title: "查找排课信息",
                        iconCls: 'icon-search',
                        width: 350,
                        height: 180,
                        modal: true,
                        buttons: [
                            {
                                text: '提交',
                                iconCls: 'icon-ok',
                                handler: function () {
                                    var courseName = $("#search_course").val();

                                    var teacherName = $("#search_teacher").val();
                                    $("#schedule_datagrid").datagrid("options").pageNumber = 1;
                                    $("#schedule_datagrid").datagrid('getPager').pagination({pageNumber: 1});

                                    $('#schedule_datagrid').datagrid({
                                        url: 'schedule/list/current/conditions',
                                        queryParams: {
                                            courseName: $("#search_course").val(),
                                            teacherName: $("#search_teacher").val()
                                        }
                                    });
                                    $("#search_course").val("");
                                    $("#search_teacher").val("");
                                    $("#search_dialog").dialog("close");
                                }
                            }
                        ]

                    });
                }
            } ,
            {
                text: '刷新',
                iconCls: 'icon-reload',
                handler: function (){
                    $("#schedule_datagrid").datagrid("options").pageNumber = 1;
                    $("#schedule_datagrid").datagrid('getPager').pagination({pageNumber: 1});
                    $('#schedule_datagrid').datagrid({
                        url: 'schedule/list/current'
                    });
                }
            }
        ],
        detailFormatter: function (index, row) {
            return '<div style="padding:2px"><table id="ddv-' + index + '"></table></div>';
        },
        onExpandRow: function (index, row) {
            $('#ddv-' + index).datagrid({
                url: 'elective/list/situation/' + row.id,
                fitColumns: true,
                singleSelect: true,
                rownumbers: true,
                height: 'auto',
                columns: [
                    [
                        {field: 'electiveWeek', width: 150, align: 'center', title: '课程周期',
                            formatter: function (value, row, index) {
                                return {
                                    1: "周一",
                                    2: "周二",
                                    4: "周三",
                                    8: "周四",
                                    16: "周五",
                                    32: "周六",
                                    64: "周日"
                                }[value] || undefined;
                            }
                        },
                        {field: 'electiveTime', width: 150, align: 'center', title: '课程节次',
                            formatter: function (value, row, index) {
                                return  {
                                    1: "第一节课",
                                    2: "第二节课",
                                    4: "第三节课",
                                    8: "第四节课",
                                    16: "第五节课",
                                    32: "第六节课",
                                    64: "第七节课",
                                    128: "第八节课"
                                }[value] || undefined;
                            }
                        },
                        {field: 'currentNum', width: 150, align: 'center', title: '当前选课人数',
                            styler: function (value, row, index) {
                                return 'color:red;';
                            }
                        } ,
                        {field: 'schedulingId', width: 100, align: 'center',
                            formatter: function (detailValue, detailRow, detailIndex) {
                                if (detailValue > 0) {
                                    $('#ddv-' + index).oneTime('0.2s', function () {
                                        $("#clear_elective_data_" + detailValue + "_" + detailIndex).linkbutton({
                                            iconCls: "icon-remove",
                                            plain: true
                                        }).click(function () {
                                                $.messager.confirm("清除数据确认",
                                                    "从选课情况中清除选课数据会导致学生选择该课程的相关信息将会丢失，请确认清除数据？",
                                                    function (r) {
                                                        if (r) {
                                                            $.ajax({
                                                                type: "POST",
                                                                url: "elective/clear/course/elective/data/" + detailRow.schedulingId + "/" + detailRow.electiveWeek + "/" + detailRow.electiveTime,
                                                                dataType: "json",
                                                                cache: false,
                                                                success: function (data) {
                                                                    $.messager.alert("操作提示", "数据清除成功！", "info");
                                                                    $('#ddv-' + index).datagrid("deleteRow", detailIndex);
                                                                }
                                                            });
                                                        }
                                                    });
                                            });
                                    });
                                    return "<a id='clear_elective_data_" + detailValue + "_" + detailIndex + "' href='#'>清除选课数据</a>";
                                }
                            }
                        }
                    ]
                ],
                onResize: function () {
                    $('#schedule_datagrid').datagrid('fixDetailRowHeight', index);
                },
                onLoadSuccess: function () {
                    setTimeout(function () {
                        $('#schedule_datagrid').datagrid('fixDetailRowHeight', index);
                    }, 0);
                },
                onClickRow: function (rowIndex, rowData) {
                    $('#elective_situation').datagrid({
                        title:row.courseName + "-" + ({
                            1: "周一",
                            2: "周二",
                            4: "周三",
                            8: "周四",
                            16: "周五",
                            32: "周六",
                            64: "周日"
                        }[rowData.electiveWeek] || undefined) + "-" + ({
                            1: "第一节课",
                            2: "第二节课",
                            4: "第三节课",
                            8: "第四节课",
                            16: "第五节课",
                            32: "第六节课",
                            64: "第七节课",
                            128: "第八节课"
                        }[rowData.electiveTime] || undefined) + "-考勤情况",
                        url:"study/list/course/" + rowData.schedulingId + "/log/" + rowData.electiveWeek + "/" + rowData.electiveTime + "/situation/"
                    });
//
//                    $.ajax({
//                        type: 'GET',
//                        url: "study/list/course/" + rowData.schedulingId + "/log/" + rowData.electiveWeek + "/" + rowData.electiveTime + "/situation/",
//                        dataType: "json",
//                        cache: false,
//                        success: function (data) {
//                            if (data) {
//                                $('#elective_situation').datagrid("getPanel").panel("setTitle", row.courseName + "-" + ({
//                                    1: "周一",
//                                    2: "周二",
//                                    4: "周三",
//                                    8: "周四",
//                                    16: "周五",
//                                    32: "周六",
//                                    64: "周日"
//                                }[rowData.electiveWeek] || undefined) + "-" + ({
//                                    1: "第一节课",
//                                    2: "第二节课",
//                                    4: "第三节课",
//                                    8: "第四节课",
//                                    16: "第五节课",
//                                    32: "第六节课",
//                                    64: "第七节课",
//                                    128: "第八节课"
//                                }[rowData.electiveTime] || undefined) + "-考勤情况");
//                                $('#elective_situation').datagrid("loadData", data);
//                            }
//                        }
//                    });
                }
            });
            $('#schedule_datagrid').datagrid('fixDetailRowHeight', index);
        },
        columns: [
            [
                {field: 'courseId', width: 200, align: 'center', title: '课程名称',
                    formatter: function (value, row, index) {
                        if (value == 0) {
                            return null;
                        }
                        return row.courseName;
                    }
                },
                {field: 'teacherId', width: 150, align: 'center', title: '授课老师',
                    formatter: function (value, row, index) {
                        if (value == 0) {
                            return null;
                        }
                        return row.teacherName;
                    }
                },
                {field: 'classroomId', width: 180, align: 'center', title: '授课地点',
                    formatter: function (value, row, index) {
                        if (value == 0) {
                            return null;
                        }
                        return row.classroomLocation;
                    }
                },
                {field: 'restrictionNumber', width: 80, align: 'center', title: '开班人数'}
            ]
        ],
        loadFilter: pageLoadFilter
    });

    $("#elective_situation").datagrid({
        fit: true,
        rownumbers: true,
        singleSelect: true,
        pagination: true,
        pageSize: 10,
        title: "考勤情况",
        columns: [
            [
                {field: 'logDate', width: 150, align: 'center', title: '考勤时间'},
                {field: 'attendanceYes', width: 150, align: 'center', title: '出勤人数'},
                {field: 'attendanceNo', width: 150, align: 'center', title: '未出勤人数'},
                {field: 'attendanceAll', width: 150, align: 'center', title: '考勤人数'}
            ]
        ],
        loadFilter: pageLoadFilter
    });
});
